<?php

namespace app\api\controller\port;

use app\common\controller\Api;

use think\Exception;

/**
 * 乘客接口
 * 
 */
class Passenger extends Api
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    /**
     * 添加乘客
     * 
     */
    public function addPassenger()
    {
        $is_default = I('post.is_default',0);
        $params = request()->post();
        $rule = [
            'name|姓名' => 'require|length:2,50',
            'mobile|手机号' => 'require|regex:/^1[3-9]{1}[0-9]{9}$/',
            // 'idcard|身份证号' => "require|regex:/^\d{6}(19|2\d)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)?$/",
            'idcard|身份证号' => "require",
        ];
        $message = [
            'mobile.require' => '请填写手机号',
            'mobile.regex' => '手机号格式不正确',
        ];
        //验证规则
        $validate = validate()->rule($rule)->message($message);
        $validate->check($params) || $this->error($validate->getError());
        $preg = '/^\d{6}(19|2\d)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)?$/';
        preg_match($preg,$params['idcard']) || $this->error('身份证号格式不正确');

        $uid = $this->auth->id;
        $have = db('passenger')->where('uid',$uid)->count();

        try {
            db()->startTrans();

            $insert = [
                'uid' => $uid,
                'name' => $params['name'],
                'mobile' => $params['mobile'],
                'idcard' => $params['idcard'],
            ];
            if($have && $is_default == 1){
                db('passenger')->where(['uid'=>$uid,'is_default'=>1])->update(['is_default'=>0]);
                $insert['is_default'] = 1;
            }
            if(!$have){
                $insert['is_default'] = 1;
            }

            db('passenger')->insert($insert);

            db()->commit();
            $this->success(__('添加成功'));
        } catch (Exception $th) {
            db()->rollback();
            $this->error($th->getMessage()?:'操作失败');
        }
    }


    /**
     * 修改乘客
     * 
     */
    public function editPassenger()
    {
        $is_default = I('post.is_default',0);
        $params = request()->post();
        $rule = [
            'id|ID参数' => 'require',
            'name|姓名' => 'require|length:2,50',
            'mobile|手机号' => 'require|regex:/^1[3-9]{1}[0-9]{9}$/',
            // 'idcard|身份证号' => "require|regex:/^\d{6}(19|2\d)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)?$/",
            'idcard|身份证号' => "require",
        ];
        $message = [
            'mobile.require' => '请填写手机号',
            'mobile.regex' => '手机号格式不正确',
        ];
        //验证规则
        $validate = validate()->rule($rule)->message($message);
        $validate->check($params) || $this->error($validate->getError());
        $preg = '/^\d{6}(19|2\d)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)?$/';
        preg_match($preg,$params['idcard']) || $this->error('身份证号格式不正确');

        $uid = $this->auth->id;
        $have = db('passenger')->where(['id'=>$params['id'],'uid'=>$uid])->find();
        empty($have) && $this->error('没有找到乘客信息');

        try {
            db()->startTrans();

            $update = [
                'name' => $params['name'],
                'mobile' => $params['mobile'],
                'idcard' => $params['idcard'],
                'is_default' => $is_default
            ];
            if($is_default == 1){
                db('passenger')->where(['uid'=>$uid,'is_default'=>1])->update(['is_default'=>0]);
            }

            db('passenger')->where(['id'=>$params['id'],'uid'=>$uid])->update($update);

            db()->commit();
            $this->success(__('修改成功'));
        } catch (Exception $th) {
            db()->rollback();
            $this->error($th->getMessage()?:'操作失败');
        }
    }


    /**
     * 乘客列表
     *
     */
    public function getPassengerList()
    {
        $limit = I('post.limit',10);
        $uid = $this->auth->id;
        $list = db('passenger')->where('uid',$uid)->order('id desc')->paginate($limit);

        $this->success('Success', $list);
    }

    /**
     * 获取默认乘客
     *
     */
    public function getDefaultPassenger()
    {
        $default = db('passenger')->where(['uid'=>$this->auth->id,'is_default'=>1])->find();
        if(empty($default)){
            $default = db('passenger')->where(['uid'=>$this->auth->id])->order('id desc')->find();
        }
        $this->success('Success', $default);
    }

    /**
     * 切换默认乘客
     *
     */
    public function switchDefaultPassenger()
    {
        $id = I('post.id');
        $uid = $this->auth->id;
        try {
            db()->startTrans();

            $have = db('passenger')->where(['id'=>$id,'uid'=>$uid])->count();
            $have || exception('没有找到乘客信息');
            db('passenger')->where(['uid'=>$uid,'is_default'=>1])->update(['is_default'=>0]);
            db('passenger')->where(['id'=>$id,'uid'=>$uid])->update(['is_default'=>1]);

            db()->commit();
            $this->success(__('操作成功'));
        } catch (Exception $th) {
            db()->rollback();
            $this->error($th->getMessage()?:'操作失败');
        }
    }


    /**
     * 删除乘客信息
     * 
     */
    public function delPassenger()
    {
        $id = I('post.id');
        $uid = $this->auth->id;

        try {
            db()->startTrans();

            $one = db('passenger')->where(['id'=>$id,'uid'=>$uid])->find();
            empty($one) && exception('没有找到乘客信息');
            //删除
            db('passenger')->where(['id'=>$id,'uid'=>$uid])->delete();
            if($one['is_default'] == 1){
                $default = db('passenger')->where(['uid'=>$this->auth->id])->order('id desc')->find();
                if(!empty($default)){
                    db('passenger')->where(['id'=>$default['id'],'uid'=>$uid])->update(['is_default'=>1]);
                }
            }

            db()->commit();
            $this->success(__('操作成功'));
        } catch (Exception $th) {
            db()->rollback();
            $this->error($th->getMessage()?:'操作失败');
        }

    }

}
